/*

Copyright (c) 2007 Niel M. Bornstein

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

using System;
using System.Collections;
using System.Text;

using Dottlr;

namespace DopplrEditTrip
{
	class MainClass
	{ 
		public static void Main(string[] args)
		{
			for (int i = 0; i < args.Length; i++) 
			{
				switch(args[i]) 
				{
					case "-h":
					case "-help":
					case "--help":
					case "help":
						Console.WriteLine("Usage: ");
						Console.WriteLine("DopplrEditTrip -t token");
						Console.WriteLine("\ttoken (required): Dopplr AuthSub token");
						return;
					case "-t":
					case "-token":
						Api.Token = args[++i];
						break;
				}
			}
			
			if (!Api.IsInitialized) {
				Console.Error.WriteLine("You must supply a Dopplr API token with the -t command line switch.");
				return;
			}
			
			// list future trips
			// if there is only one, assume that that's the one
			FutureTripsInfo trips = Api.DoFutureTripsInfo();
			int selection = 1;
			if (trips.Trip.Length == 0) {
				Console.Error.WriteLine("No trips found.");
				return;
			} else if (trips.Trip.Length > 1) {
				Console.WriteLine("Trips found:");
				for (int i = 0; i < trips.Trip.Length; i++) {
					Trip t = trips.Trip[i];
					Console.WriteLine("({0}) {1})", i+1, t); 
				}
				Console.Write("Enter selection [1] > ");
				string selectStr = Console.ReadLine();
				if (selectStr.Length > 0) {
					selection = int.Parse(selectStr);
				}
			}
			TripInfo trip = Api.DoTripInfo(trips.Trip[selection-1].Id);
			
			// airport tag
			Console.Write("What airport will you be flying into? (just hit enter if none) ");
			string airport = Console.ReadLine().ToUpper();
			if (airport.Length == 3) {
				Api.DoAddTripTags(trip.Trip.Id, string.Format("airport:iata={0}", airport));
			} else {
				Console.Error.WriteLine("IATA airport codes consist of three letters.");
			}
			
			// flight tag
			bool done = false;
			while (!done) {
				Console.WriteLine("What airline and flight will you be taking? Use format AAnnnn, where AA is the IATA code and nnnn is the flight number. (you will be able to enter additional flights, just hit enter when done) ");
				string flight = Console.ReadLine().ToUpper();
				if (flight.Length > 0) {
					Api.DoAddTripTags(trip.Trip.Id, string.Format("flight:iata={0}", flight));
				} else {
					done = true;
				}
			}
			
			// upcoming tag
			Console.Write("What Upcoming <http://upcoming.yahoo.com/> event are you travelling to? (Just hit enter if none) ");
			string upcoming = Console.ReadLine();
			if (upcoming.Length > 0) {
				Api.DoAddTripTags(trip.Trip.Id, string.Format("upcoming:event={0}", upcoming));
			}
			
			// custom tags
			Console.Write("Enter any additional tags for this trip, separated by spaces (Just hit enter if none) ");
			string tags = Console.ReadLine();
			if (tags.Length > 0) {
				Api.DoAddTripTags(trip.Trip.Id, tags);
			}
			
			// get the final trip
			TripInfo tripInfo = Api.DoTripInfo(trip.Trip.Id);
			Console.WriteLine("{0} with tags:", tripInfo.Trip);
			foreach (string tag in tripInfo.Trip.Tag) {
				Console.WriteLine("\t{0}", tag);
			}
		}
	}
}